package com.hyg.common.utils;

import android.media.AudioManager;
import android.os.Build;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
import android.text.TextUtils;

import com.hyg.common.CommonLibary;
import com.hyg.common.utils.language.LanguageUtil;
import com.hyg.common.utils.language.SPLanguageUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * FileName: TTSUtil
 * Author: huangyuguang
 * Date: 2022/6/7
 * Description: 文字转语音，需要安装科大讯飞语音引擎，并且在语言设置把tts选择科大讯飞
 */
public class TTSUtil implements TextToSpeech.OnInitListener {

    private static TTSUtil instance;

    private List<String> stringList = new ArrayList<>();
    private TextToSpeech mTextToSpeech;
    private boolean isInitSuccess = false;

    private int currentPlayVol = 0;

    private boolean isPlayMusic = false;

    public static TTSUtil getInstance(){
        if(instance == null){
            synchronized (TTSUtil.class){
                if(instance == null){
                    instance = new TTSUtil();
                }
            }
        }
        return instance;
    }

    public TTSUtil() {
        mTextToSpeech = new TextToSpeech(CommonLibary.getContext(),this,"com.iflytek.speechcloud");
        mTextToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
            @Override
            public void onStart(String utteranceId) {
                //朗读中
            }

            @Override
            public void onDone(String utteranceId) {
                //朗读结束
                speakNext();
            }

            @Override
            public void onError(String utteranceId) {
                //朗读失败
                speakNext();
            }
        });
        // 设置音调，值越大声音越尖（女生），值越小则变成男声,1.0是常规
        mTextToSpeech.setPitch(1.0f);
        // 设置语速
        mTextToSpeech.setSpeechRate(0.9f);
    }

    public void addSpeak(int stringResId){
        addSpeak(LanguageUtil.getLocalString(CommonLibary.getContext(),stringResId));
    }

    public void addSpeak(String str){
        if(TextUtils.isEmpty(str)) return;

        currentPlayVol = AudioManagerUtil.getCurrentVol(AudioManager.STREAM_MUSIC);
        AudioManagerUtil.setPlayVol(1000000);

        if(!mTextToSpeech.isSpeaking() && isInitSuccess){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                mTextToSpeech.speak(str, TextToSpeech.QUEUE_FLUSH, null,"123456");
            }else {
                mTextToSpeech.speak(str, TextToSpeech.QUEUE_FLUSH, null);
            }
        }else {
            stringList.add(str);
        }
    }

    private void speakNext(){
        if(stringList.size() > 0){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                mTextToSpeech.speak(stringList.remove(0), TextToSpeech.QUEUE_FLUSH, null,"123456");
            }else {
                mTextToSpeech.speak(stringList.remove(0), TextToSpeech.QUEUE_FLUSH, null);
            }
        }else {
            AudioManagerUtil.setPlayVol(currentPlayVol);

        }
    }

    public void release(){
        if(mTextToSpeech !=null){
            mTextToSpeech.stop();
            mTextToSpeech.shutdown();
        }
    }

    @Override
    public void onInit(int status) {
        // 判断是否转化成功
        if (status == TextToSpeech.SUCCESS) {
            isInitSuccess = true;
            if(SPLanguageUtil.getInstance(CommonLibary.getContext()).getSelectLanguage() == 0){
                int result = mTextToSpeech.setLanguage(Locale.CHINESE);
                if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                    ToastUtils.show("Language is not available.");
                    mTextToSpeech.setLanguage(Locale.US);
                }
            }else {
                mTextToSpeech.setLanguage(Locale.US);
            }
            speakNext();
        }
    }

}
